package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @package com.leyou.item.service
 * @description: 操作品牌的业务层
 * @author: 许超越
 * @date 2019/6/25 14:51
 * @version: V1.0
 */
@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 后台商品分页查询
     *
     * @return
     * @param: key 模糊查询关键字
     * @param: page 当前页码
     * @param: rows 每页显示条数
     * @param: sortBy 排序设置关键字段
     * @param: desc 是否降序排序
     */
    public PageResult<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, boolean desc) {
        //分页
        PageHelper.startPage(page, rows);
        //创建过滤条件
        Example example = new Example(Brand.class);
        //使用工具类过滤传递的参数
        //key 模糊查询
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key.toUpperCase());
        }
        //sortBy 排序
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));
        }
        //查询结果
        List<Brand> brands = brandMapper.selectByExample(example);
        //对查询结果判断是否为空
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //对查询结果解析
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        //将将查询结果转换成BrandDTO类型
        List<BrandDTO> brandDTOList = BeanHelper.copyWithCollection(brands, BrandDTO.class);
        //返回结果
        return new PageResult<BrandDTO>(pageInfo.getTotal(), brandDTOList);
    }


    /**
     * 后台新增商品业务层
     * @param: brandDTO 商品信息的分装类
     * @param: cids 商品分类的id
     */
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        //将数据转换成和数据库中一一对应的实体类
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);
        //进行商品的新增
        int i = brandMapper.insertSelective(brand);
        //对结果进行判断
        if (i != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //新增品牌和类型的中间表，批量操作
        int count = brandMapper.insertCategoryBrand(brand.getId(), cids);
    }


    /**
     * 修改品牌和类目信息
     * @param: brandDTO
     * @param: cids
     */
    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {
        //将brandDTO转换成brand
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        //根据brand主键修改内容
        int updateCount = brandMapper.updateByPrimaryKey(brand);
        //判断是否更新成功
        if (updateCount != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        //删除原先的中间表
        int deleteCount = brandMapper.deleteIntermediateByBrandId(brand.getId());
        if (deleteCount != cids.size()) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        //从新插入中间表
        int insertCount = brandMapper.insertCategoryBrand(brand.getId(), cids);
        if (insertCount != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    /**
     * 通过品牌id查询商品品牌信息
     * @param: id 品牌id
     * @return
     */
    public BrandDTO queryById(Long id) {
        //查询品牌信息
        Brand brand = brandMapper.selectByPrimaryKey(id);
        //判断是否查询到
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }


    /**
     * 根据商品分类查询品牌信息
     * @param: cid 商品分类id
     * @return
     */
    public List<BrandDTO> queryBrandByCid(Long cid) {
        //根据cid查询对应的商品信息
        List<Brand> brandList = brandMapper.queryBrandByCid(cid);
        //判断是否查询到
        if (CollectionUtils.isEmpty(brandList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList, BrandDTO.class);
    }

    /**
     * 根据品牌id集合查询品牌
     * @param: idList 品牌分类id集合
     * @return
     */
    public List<BrandDTO> queryBrandByIds(List<Long> idList) {
        List<Brand> brandList = brandMapper.selectByIdList(idList);
        return BeanHelper.copyWithCollection(brandList, BrandDTO.class);
    }
}
